/**
 *
 * 怪物基类
 * @author 
 *
 */
class TowersBase extends VectorElements implements ILife{
    /**单一目标*/
    public target: any;    
    /**引用场景中的对象层*/
    public contentLayer: egret.Sprite;
    /**进入到士兵攻击范围的敌人数组*/
    public atargets: any[];
    /**是否移动到目标*/
    protected moveToTarget: boolean = false;
    
    /**先左或先右*/
    protected firstst: string = "R";
    /**射击方向*/
    protected direct: string;
    
    /**弓箭瞄向目标向上偏移量(自身头部位置)*/
    public offy: number;
    
    /**当前生命力*/
    protected _hp: number;
    /**当前生命力*/
    protected time: number=0;
    /**最大生命力*/
    public life: number;
    
    public sx: number;
    public sy: number;
    
    /**状态标签*/
    protected stateLabel: string;
    /**开火延迟*/
    protected fireDelay: number=0;
    /**时间累计*/
    protected timesum: number=0;
    
    /**选中状态管理*/
    protected selectYQ: egret.Shape;
    
    /**血条*/
    protected lifeBar: LifeBar;
    /**view MovieClip*/
    public view: egret.MovieClip;
    /**自动恢复hp(按百分比恢复生命)*/
    protected addhp: number;
    
    //相对于集结点的偏移量
    public xoffset: number=0;
    public yoffset: number=0;
    /**起始点（集结点）*/
    protected jijieDian: Vector2D;
    /**是否在集结点*/
    public atjijie: boolean = false;
    
    /**射程范围最大半径*/
    public maxRadius: number = 140;
    /**射程范围最小半径*/
    public minRadius: number = 100;
    /**将圆沿y轴压扁变为椭圆时候的比例*/
    public ratioY: number;

    /**攻击音效数组*/
    protected voiceArr: string[] = ["shield_fire1","shield_fire2","shield_fire3"];
    
    
	public constructor() {
        super();
        //血条
        this.lifeBar = new LifeBar();
        this.addChild(this.lifeBar);
	}
		
    /**播放射击音效*/
    protected playFireVoice() {
        if(Math.random() > 0.5)
            SoundManager.playEffect("arrow_fire1");
        else
            SoundManager.playEffect("arrow_fire2");
    }
	
    /**检测生命*/
    public live() { 
        if(this._hp <= 0) {
            this.canClear = true;
        }
        if(this._hp > this.life) { 
            this._hp = this.life;
        }
    }
    /**生命值变动*/
    public set hp(value:number){
        this._hp = value;
        this.live();
        this.lifeBar.setProgress(this._hp,this.life);
    }
    public get hp():number{
        return this._hp;
    }
    
    public createYQ(){
        //console.log("生成圆圈");
        /*
        this.selectYQ = Utils.drawEllipse(15,8,0x009900);
        this.selectYQ.x = 30;
        this.selectYQ.y = 30;
        this.addChildAt(this.selectYQ,0);
        */
        this.view.touchEnabled = false;
    }
    public clearYQ(){
        //console.log("取消圆圈");
        if(this.selectYQ != null){
            this.removeChild(this.selectYQ);
            this.selectYQ = null;
        }
        this.view.touchEnabled = true;
    }
    
    /**销毁*/
    public onDestroy():void {
        super.onDestroy();
        if(this.target != null){
            this.target.target = null;
            this.target = null;
        }
    }
    
//    /**碰撞检测*/
//    protected hittest(){
//        if(this.target != null) {
//            this.target.hp -= this.damage;
//            if(this.target.hp <= 0) {
//                var idx: number = this.targets.indexOf(this.target);
//                if(idx != -1)
//                    this.targets.splice(idx,1);
//                idx = this.atargets.indexOf(this.target);
//                if(idx != -1)
//                    this.atargets.splice(idx,1);
//                this.target.target = null;
//                this.target = null;
//            }
//        }
//    }
	
    
    /**-----------------------------------------------------------------帧事件----------------------------------------------------------------------*/
    public onEnterFrame(advancedTime:number){
        //攻击间隔
        this.timesum += advancedTime;
        //向量刷新(移动)
        super.onEnterFrame(advancedTime);
        //clear
        if(this.canClear){
            ObjectPool.getInstance().destroyObject(this);
        }
        
        var i: number;
        this.atargets = [];
        for(i = 0;i < this.targets.length;i++) {
            var obj = this.targets[i];
            if(obj.target == null) {
                var isin: boolean = Utils.containsXY(obj.x,obj.y,this.sx,this.sy - 22,this.maxRadius,this.ratioY);
                var index = this.atargets.indexOf(obj);
                if(isin && obj.hp > 0) {
                    if(index == -1)
                        this.atargets.push(obj);
                } else {
                    if(index != -1)
                        this.atargets.splice(index,1);
                }
            }
        }
        
        //排序、敌人的攻击对象不为null时则排到末尾
        for(i = 0;i < this.targets.length;i++) {
            var obj = this.targets[i];
            if(obj.target != null) {
                var isin: boolean = Utils.containsXY(obj.x,obj.y,this.sx,this.sy - 22,this.maxRadius,this.ratioY);
                var index = this.atargets.indexOf(obj);
                if(isin && obj.hp > 0) {
                    if(index == -1)
                        this.atargets.push(obj);
                } else {
                    if(index != -1)
                        this.atargets.splice(index,1);
                }
            }
        }
    }
            
    ////////////////////////////////状态切换
    /**闲置*/
    public isIdle():boolean{
        this.stateLabel = "idle";
        return true;
    }
    /**移动*/
    public isMove():boolean{
        //this.stateLabel = "walking";
        return true;
    }
    /**攻击*/
    public isFighting():boolean{
        this.stateLabel = "fighting";
        return true;
    }
    /**死亡*/
    public isDead():boolean{
        this.stateLabel = "dead";
        return true;
    }
    
}
